//
//  YZYNavigationViewController.swift
//  Y微Z博Y
//
//  Created by mac on 16/10/22.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit

class YZYNavigationController: UINavigationController, UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 当程序员自定义导航的返回按钮后 屏幕左侧边缘右滑动的时候失效
        interactivePopGestureRecognizer?.delegate = self
    }
    
    @objc private func backClick() {
        popViewController(animated: true)
    }
    
    // 重写父类的push方法
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        var title: String
        // 由于消息 发现 我 都有返回按钮
        if childViewControllers.count > 0 {
            title = "返回"
            // 如果是第一次 push
            if childViewControllers.count == 1 {
                // 等于首页的title
                title = childViewControllers.first?.title ?? ""
            }
            // 设置导航的左侧按钮
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(setHighlightedImgName: "navigationbar_back_withtext", title: title, target: self, action: #selector(backClick))
        }
        super.pushViewController(viewController, animated: animated)
    }
    
    // 如果是首页的话 然后用户又向右侧滑动 然后在点击 push 不能完成
    // 默认如果不实现 返回的均为 true
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
        // 如果. count == 1 就代表你已经到首页了 不能在滑动了
        return childViewControllers.count != 1
    }

}
